package com.lgs7907.simplestockinfo;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class StockInfoActivity extends Activity {
	private StockDb mStockDb;
	private int position;
	private ImageView iv_canvas;
	private SimpleInfo simpleInfo;
	private Button btn_5min_line;
	private Button btn_day_line;
	//private Button btn_week_line;
	private Button btn_month_line;
	private int currentSel = 0;
	private TextView tv_basic_info;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_stock_info);
		Intent intent = getIntent();
		position = intent.getIntExtra("POSITION", 0);

		tv_basic_info = (TextView) findViewById(R.id.tv_basic_info);
		iv_canvas = (ImageView) findViewById(R.id.iv_canvas);
		btn_5min_line = (Button) findViewById(R.id.btn_5min_line);
		btn_day_line = (Button) findViewById(R.id.btn_day_line);
		//btn_week_line = (Button) findViewById(R.id.btn_week_line);
		btn_month_line = (Button) findViewById(R.id.btn_month_line);
		btn_5min_line.setOnClickListener(new clickListener());
		btn_day_line.setOnClickListener(new clickListener());
		//btn_week_line.setOnClickListener(new clickListener());
		btn_month_line.setOnClickListener(new clickListener());

		mStockDb = MyApplication.getStockDb();
		simpleInfo = getInfo(position);
		setTitle(simpleInfo.stock_name);

		tv_basic_info.setText("当前价格：" + simpleInfo.stock_current_price + "\t\t涨跌幅：" + simpleInfo.stock_current_change);
		showChart(0);
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
			// setContentView(R.layout.file_list_landscape);// 横向
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);// 设置竖屏
		} else {
			// setContentView(R.layout.file_list);// 竖向
		}
	}

	class clickListener implements OnClickListener {
		public void onClick(View v) {

			if (v.getId() == R.id.btn_5min_line) {
				showChart(0);
			} else if (v.getId() == R.id.btn_day_line) {
				showChart(1);
			} else if (v.getId() == R.id.btn_month_line) {
				showChart(3);
			}
		}
	}

	private void showChart(int type) {
		switch (currentSel) {
		case 0:
			btn_5min_line.setText("五分钟线");//必须重新设置文字，才能取消下划线
			btn_5min_line.getPaint().setFlags(0);
			break;
		case 1:
			btn_day_line.setText("日线");
			btn_day_line.getPaint().setFlags(0);
			break;
		case 2:
			break;
		case 3:
			btn_month_line.setText("月线");
			btn_month_line.getPaint().setFlags(0);
			break;
		}
		currentSel = type;
		
		String imageUrl = "";
		switch (type) {
		case 0:
			btn_5min_line.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
			imageUrl = simpleInfo.chart_minute;
			break;
		case 1:
			btn_day_line.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
			imageUrl = simpleInfo.chart_daily;
			break;
		case 2:
			imageUrl = simpleInfo.chart_weekly;
			break;
		case 3:
			btn_month_line.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
			imageUrl = simpleInfo.chart_monthly;
			break;
		}
		BitmapDownloaderTask task = new BitmapDownloaderTask(new BitmapDownloaderTask.ICallback() {
			@Override
			public void afterPost(Bitmap bitmap, String idString) {
				if (bitmap != null) {
					iv_canvas.setImageBitmap(bitmap);
				}
			}
		});
		task.execute(imageUrl, "");
	}

	public SimpleInfo getInfo(int pos) {
		mStockDb.lock();
		SimpleInfo info = new SimpleInfo();
		Cursor mCursor;
		mCursor = mStockDb.select();
		if (mCursor != null) {
			mCursor.moveToPosition(pos);
			info.code = mCursor.getString(0);
			info.stock_name = mCursor.getString(1);
			info.stock_current_price = mCursor.getDouble(4);
			info.stock_current_change = mCursor.getDouble(37);

			info.chart_minute = mCursor.getString(33);
			info.chart_daily = mCursor.getString(34);
			info.chart_weekly = mCursor.getString(35);
			info.chart_monthly = mCursor.getString(36);
		}
		mStockDb.unlock();

		return info;
	}
}
